
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 09/01/2013 16:41:07
-- Generated from EDMX file: C:\Users\Dima.statsenko\documents\visual studio 2012\Projects\Cdx\Cdx.Bl\Model\EngineModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Cdx];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_E_UserR_UserApplication]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[R_UserApplicationSet] DROP CONSTRAINT [FK_E_UserR_UserApplication];
GO
IF OBJECT_ID(N'[dbo].[FK_E_ApplicationR_UserApplication]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[R_UserApplicationSet] DROP CONSTRAINT [FK_E_ApplicationR_UserApplication];
GO
IF OBJECT_ID(N'[dbo].[FK_E_DocumentE_Application]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[E_ApplicationSet] DROP CONSTRAINT [FK_E_DocumentE_Application];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[E_UserSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[E_UserSet];
GO
IF OBJECT_ID(N'[dbo].[E_ApplicationSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[E_ApplicationSet];
GO
IF OBJECT_ID(N'[dbo].[R_UserApplicationSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[R_UserApplicationSet];
GO
IF OBJECT_ID(N'[dbo].[E_DocumentSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[E_DocumentSet];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'E_UserSet'
CREATE TABLE [dbo].[E_UserSet] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'E_ApplicationSet'
CREATE TABLE [dbo].[E_ApplicationSet] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Active] bit  NOT NULL
);
GO

-- Creating table 'R_UserApplicationSet'
CREATE TABLE [dbo].[R_UserApplicationSet] (
    [Id] uniqueidentifier  NOT NULL,
    [E_User_Id] uniqueidentifier  NOT NULL,
    [E_Application_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'E_DocumentSet'
CREATE TABLE [dbo].[E_DocumentSet] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Path] nvarchar(max)  NOT NULL,
    [E_Application_Id] uniqueidentifier  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'E_UserSet'
ALTER TABLE [dbo].[E_UserSet]
ADD CONSTRAINT [PK_E_UserSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'E_ApplicationSet'
ALTER TABLE [dbo].[E_ApplicationSet]
ADD CONSTRAINT [PK_E_ApplicationSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'R_UserApplicationSet'
ALTER TABLE [dbo].[R_UserApplicationSet]
ADD CONSTRAINT [PK_R_UserApplicationSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'E_DocumentSet'
ALTER TABLE [dbo].[E_DocumentSet]
ADD CONSTRAINT [PK_E_DocumentSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [E_User_Id] in table 'R_UserApplicationSet'
ALTER TABLE [dbo].[R_UserApplicationSet]
ADD CONSTRAINT [FK_E_UserR_UserApplication]
    FOREIGN KEY ([E_User_Id])
    REFERENCES [dbo].[E_UserSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_E_UserR_UserApplication'
CREATE INDEX [IX_FK_E_UserR_UserApplication]
ON [dbo].[R_UserApplicationSet]
    ([E_User_Id]);
GO

-- Creating foreign key on [E_Application_Id] in table 'R_UserApplicationSet'
ALTER TABLE [dbo].[R_UserApplicationSet]
ADD CONSTRAINT [FK_E_ApplicationR_UserApplication]
    FOREIGN KEY ([E_Application_Id])
    REFERENCES [dbo].[E_ApplicationSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_E_ApplicationR_UserApplication'
CREATE INDEX [IX_FK_E_ApplicationR_UserApplication]
ON [dbo].[R_UserApplicationSet]
    ([E_Application_Id]);
GO

-- Creating foreign key on [E_Application_Id] in table 'E_DocumentSet'
ALTER TABLE [dbo].[E_DocumentSet]
ADD CONSTRAINT [FK_E_ApplicationE_Document]
    FOREIGN KEY ([E_Application_Id])
    REFERENCES [dbo].[E_ApplicationSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_E_ApplicationE_Document'
CREATE INDEX [IX_FK_E_ApplicationE_Document]
ON [dbo].[E_DocumentSet]
    ([E_Application_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------